<template>
  <el-form ref="deptForm" label-width="120px" :rules="rules" :model="form">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
        <el-option v-for="e in list" :key="e.id" :value="e.username" :label="e.username" />

      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { departmentAdddata, departmentChangdata, departmentlookdata } from '@/api/departments'
import { getEmployeeSimple } from '@/api/employees'
export default {
  props: {
    id: {
      required: true,
      type: String
    },
    isEdit: {
      required: true,
      type: Boolean
    },
    orj: {
      required: true,
      type: Array
    }
    // [{name,id,pid,code}]
  },
  data() {
    // 同级部门禁止出现重复部门 的校验

    const valiName = (rule, val, callback) => {
      console.log('name', val)
      // 有pid的是子选项 找到这个选中id中的子选项
      const pid = this.orj.find(e => e.id === this.id).pid
      const brotherArr = this.orj.filter(e => e.pid === pid)
      const allName = brotherArr.filter(e => e.id !== this.id).map(e => e.name)

      if (allName.includes(val)) {
        callback(new Error(`部门名${val}已存在`))
      } else {
        callback()
      }
    }

    // 新增的部门编码 不能和重复
    const valiCode = (rule, val, callback) => {
      console.log('code', val)
      let allCode = this.orj.map(e => e.code) // 这里会有bug 当选中编辑时 不更改就会显示重复
      // console.log(allCode)
      // 给一个if判断  判断选中的是不是 编辑
      // 在原数据中找 如果 这个选中的 id 不等于 数组里面的id 就把除了这一项别的都筛选出来了
      if (this.isEdit) {
        allCode = this.orj.filter(e => e.id !== this.id).map(e => e.code)
      }

      if (allCode.includes(val)) {
        callback(new Error(`编码名${val}已存在`))
      } else {
        callback()
      }
    }

    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '必填', trigger: 'blur' },
          { min: 1, max: 15, message: '部门名称不能为空', trigger: 'blur' },
          { validator: valiName, trigger: 'blur' }
        ], // 部门名称
        code: [
          { required: true, message: '必填', trigger: 'blur' },
          { min: 1, max: 15, message: '部门编码不能为空', trigger: 'blur' },
          { validator: valiCode, trigger: 'blur' }
        ], // 部门编码
        manager: [
          { required: true, message: '必填', trigger: 'blur' }
        ], // 部门管理者
        introduce: [
          { required: true, message: '必填', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍不能为空', trigger: 'blur' }
        ] // 部门介绍
      },
      list: []
    }
  },
  created() {
    this.getEmployees()
    if (this.isEdit) this.getNowEdit()
  },
  methods: {
    // 确定要发送的请求
    hSubmit() {
      // 最后发送请求前的兜底校验
      this.$refs.deptForm.validate((val) => {
        if (!val) return this.$message.error('信息错误请重试')
        this.isEdit ? this.doEdit() : this.doAdd()
      })
    },
    // 取消请求
    hCancel() {
      this.$emit('off')
    },
    // 添加
    async doAdd() {
      try {
        const res = await departmentAdddata({ ...this.form, pid: this.id })
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        // console.log(res)
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },
    // 编辑
    async doEdit() {
      try {
        const res = await departmentChangdata({ ...this.form, id: this.id })
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        // console.log(res)
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },
    // 获取员工的姓名
    async getEmployees() {
      try {
        const res = await getEmployeeSimple()
        // console.log(res)
        this.list = res.data
      } catch (error) {
        console.log(error)
      }
    },
    // 获取当前选中的名单 存入 form 表单中
    async getNowEdit() {
      try {
        const res = await departmentlookdata(this.id)
        // console.log(res)
        this.form = res.data
      } catch (error) {
        console.log(error)
      }
    },
    resetForm() {
      this.$refs.deptForm.resetFields()
    }
  }
}
</script>
